/*
 * EventDispatcher.h
 *
 *  Created on: 2013-8-8
 *  Author: xnan
 */

#ifndef XNL_EVENTDISPATCHER_CLASS_H_
#define XNL_EVENTDISPATCHER_CLASS_H_

#include <map>
#include <list>
#include <stack>

#include "IEventDispatcher.h"
#include "Event.h"

NS_XNL_BEGIN

class EventDispatcher : public Object, public IEventDispatcher
{
protected:
    /*
     * 事件监听器信息
     */
    class EventListenerInfo
    {
    public:
        Object * m_target; // 实际处理事件的对象
        const char * m_eventName; // 事件名
        XNint m_eventType; // 事件类型
        XNbool m_useImmed; // 对象接到事件后是否立即处理
        XNint m_priority; // 事件的优先级(数字越小，优先级越大,支持负数)
        Object * m_userdata; // 用户自定义数据
        EventCallback m_callback; // 事件回调函数
    };
    
public:
    EventDispatcher();
    
    virtual ~EventDispatcher();
    
    virtual void addEventListener(
        Object * p_target,
        const char * p_eventName,
        XNint p_eventType,
        EventCallback p_callback,
        XNbool p_usePrev = false,
        XNint p_priority = 0,
        Object * p_userdata = NULL
    );
    
    virtual void removeEventListener(
		const char * p_eventName,
		XNint p_eventType,
		EventCallback p_callback
	);
    
    virtual void removeEventListener(
    	const char * p_eventName,
    	XNint p_eventType,
    	EventCallback p_callback,
    	XNbool p_useImmed
    );
    
    void dispatchEvent( Event * p_event );
    
protected:
    void handleEvent( Event * p_event, XNbool p_useImmed = false );
    
private:
    void clearEventListenerInfo();
    
protected:
    std::list<EventListenerInfo*> m_eventListenerInfos;
    std::stack<EventDispatcher*> m_eventChains;
};

NS_XNL_END

#endif /* XNL_EVENTDISPATCHER_CLASS_H_ */
